﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmLoaiDen : Form
    {
        public frmLoaiDen()
        {
            InitializeComponent();
        }

        private void btnThemDen_Click(object sender, EventArgs e)
        {
            try
            {
                LoaiDenDTO _ThemLoaiDen = new LoaiDenDTO();
                _ThemLoaiDen.Ten = txtLoaiMoiDen.Text.Trim();
                if (LoaiDenBUS.Add(_ThemLoaiDen))
                {
                    txtLoaiMoiDen.Text = "";
                    Reload();
                }
                else
                {
                    MessageBox.Show("Không thể thêm loại mới.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        private void btnLuuDen_Click(object sender, EventArgs e)
        {
            try
            {
                LoaiDenDTO _SuaLoaiDen = (LoaiDenDTO)lsDSLoaiDen.Items[lsDSLoaiDen.SelectedIndex];
                _SuaLoaiDen.Ten = txtTenLoaiMoiDen.Text.Trim();
                if (LoaiDenBUS.Alter(_SuaLoaiDen))
                {
                    Reload();
                }
                else
                {
                    MessageBox.Show("Không thể thay đổi thông tin loại.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
       
        void Reload()
        {
            lsDSLoaiDen.DataSource = LoaiDenBUS.LoadAll();
            lsDSLoaiDen.DisplayMember = "TEN";
            lsDSLoaiDen.ValueMember = "ID";
            lsDSLoaiDen.SelectedIndex = -1;
        }
        private void frmLoaiDen_Load(object sender, EventArgs e)
        {
            btnLuuDen.Enabled = false;
            btnThemDen.Enabled = false;
            Reload();
        }

        private void btnThoatDen_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lsDSLoaiDen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsDSLoaiDen.SelectedIndex == -1)
            {
                txtTenLoaiMoiDen.Text = "";
                txtTenLoaiMoiDen.Enabled = false;
                btnLuuDen.Enabled = false;
                return;         
            }
            txtTenLoaiMoiDen.Enabled = true;
            btnLuuDen.Enabled = true;
            txtTenLoaiMoiDen.Text = ((LoaiDenDTO)lsDSLoaiDen.Items[lsDSLoaiDen.SelectedIndex]).Ten;
        }

        private void txtLoaiMoiDen_TextChanged(object sender, EventArgs e)
        {
            if (txtLoaiMoiDen.Text.Trim() == "")
            {
                btnThemDen.Enabled = false;
            }
            else
            {
                btnThemDen.Enabled = true;
            }
        }

        private void txtTenLoaiMoiDen_TextChanged(object sender, EventArgs e)
        {
            if (txtTenLoaiMoiDen.Text.Trim() == "" || lsDSLoaiDen.SelectedIndex == -1)
            {
                btnLuuDen.Enabled = false;
            }
            else
            {
                btnLuuDen.Enabled = true;
            }
        }

        //private void frmSuaLoaiDi_Load(object sender, EventArgs e)
        //{
        //    btnDongYDi.Enabled = false;
        //    Loadcmb();
        //}

        //public void Loadcmb()
        //{
        //    cmbLoaiCuDi.DataSource = LoaiDiBUS.LoadAll();
        //    cmbLoaiCuDi.DisplayMember = "Ten";
        //    cmbLoaiCuDi.ValueMember = "ID";
        //}


        //private void cmbLoaiCuDi_DropDownClosed(object sender, EventArgs e)
        //{
        //    txtLoaiMoiDi.Enabled = true;
        //}

        //private void txtLoaiMoiDi_TextChanged(object sender, EventArgs e)
        //{
        //    string ten = txtLoaiMoiDi.Text.Trim();

        //    if (ten != "" && cmbLoaiCuDi.Text != "")
        //    {
        //        _canDongY = true;
        //        btnDongYDi.Enabled = true;
        //    }
        //    else
        //    {
        //        _canDongY = false;
        //        btnDongYDi.Enabled = false;
        //    }
        //}

        //private void btnDongYDi_Click(object sender, EventArgs e)
        //{
        //    if (!_canDongY)
        //        return;

        //    String ten = txtLoaiMoiDi.Text.Trim();
        //    LoaiDiDTO tmp = new LoaiDiDTO();
        //    tmp.ID = int.Parse(cmbLoaiCuDi.SelectedValue.ToString());
        //    tmp.Ten = ten;

        //    if (LoaiDiBUS.Alter(tmp))
        //    {
        //        Loadcmb();
        //        txtLoaiMoiDi.Text = "";
        //    }
        //    else
        //    {
        //        MessageBox.Show("không thay đổi đc");
        //    }
        //}

        //private void btnHuyDi_Click(object sender, EventArgs e)
        //{
        //    this.Close();
        //}
    }
}
